﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HA.ModFlow.Controls.Windows.Grids
{
    /// <summary>
    /// winFlowBudget.xaml 的交互逻辑
    /// </summary>
    public partial class winFlowBudget : Window
    {
        public winFlowBudget(HA.Data.DSModflow dsFlow, int index, List<int[]> selectCellNumbers)
        {

            InitializeComponent();
            this._dsFlow = dsFlow;
            this._index = index;
            _selectCellNumbers = selectCellNumbers;
            this.labSelect.Text = "选择的单元数:" + selectCellNumbers.Count ;
        }


        private HA.Data.DSModflow _dsFlow;

        private int _index;
        private List<int[]> _selectCellNumbers;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this._dsFlow == null)
            {
                //出错
            }
            List<HA.Data.FlowBudget> result = null;
            if (this._selectCellNumbers.Count > 0)
            {
                result = this._dsFlow.CalFlowBudget(this._index,this._selectCellNumbers);
            }
            else
            {
               result = this._dsFlow.CalFlowBudget(this._index);
            }

            string[] str = { "WELL", "RIVER", "DRAIN", "PGeneralHeadBoundary", "PRecharge", "PEvapotranspiration", "PLayerPropertyFlow", "CONSTANT HEAD", "FLOW RIGHT FACE", "FLOW FRONT FACE" };
            List<HA.Data.FlowBudget> result2 = new List<Data.FlowBudget>();
            foreach (string  st in str)
            {
                result2 = GetResult(result, st);
            }
          

            GridView gridView = new GridView();

            GridViewColumn view = new GridViewColumn();
            view.Header = "名称";
            Binding bin = new Binding("Name");
            bin.Converter = new ENToCNConverter();
            view.DisplayMemberBinding = bin;
            gridView.Columns.Add(view);

            view = new GridViewColumn();
            view.Header = "流入";
            bin = new Binding("FlowIn");
            view.DisplayMemberBinding = bin;
            gridView.Columns.Add(view);

            view = new GridViewColumn();
            view.Header = "流出";
            bin = new Binding("FlowOut");
            view.DisplayMemberBinding = bin;
            gridView.Columns.Add(view);

            this.lstView.View = gridView;
            this.lstView.ItemsSource = result2;
        }

        private List<HA.Data.FlowBudget> GetResult(List<HA.Data.FlowBudget> flowbudg, string name)
        {
            List<HA.Data.FlowBudget> arry = flowbudg;
            if (arry == null)
            {
                return null;
            }
            bool p = false;           
            for (int i = 0; i < flowbudg.Count; i++)
            {
                if (flowbudg[i].Name.Contains(name))
                {
                    p = true;                  
                }
            }
            if (!p)
            {
                arry.Insert(0, new Data.FlowBudget(name, null, null));
            }
            return arry;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

    class ENToCNConverter : IValueConverter
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string enStr = (string)value;

            switch (enStr)
            {
                case "WELL":
                    return "井";
                case "RIVER":
                    return "河流";
                case "DRAIN":
                    return "排水渠";
                case "PGeneralHeadBoundary":
                    return "通用水头";
                case "PRecharge":
                    return "补给";
                case "PEvapotranspiration":
                    return "蒸散";
                case "PLayerPropertyFlow":
                    return "层属性流动";
                case "CONSTANT HEAD":
                    return "定水头";
                case "FLOW RIGHT FACE":
                    return "右侧流量";
                case "FLOW FRONT FACE":
                    return "正面流量";
                case "FLOW LOWER FACE":
                    return "底部流量";
                case "STORAGE":
                    return "储水";


                default:
                    return enStr;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
